package com.it.bigdata.schedual.imp;

import com.it.bigdata.schedual.Schedual;
import com.it.bigdata.util.HttpJedis;
import redis.clients.jedis.Jedis;

public class BDTBSchedualForRedis implements Schedual {
    public static final String CURRENTPAGENUM = "currentPageNum";
    public static final String PAGELIST = "pageList";

    private Long currentPageNum = 0L;
    //使用单例模式
    private BDTBSchedualForRedis(){}

    private static  BDTBSchedualForRedis bdtbScheduaFroRedis=new BDTBSchedualForRedis();

    public static BDTBSchedualForRedis getBdtbSR(){
        return bdtbScheduaFroRedis;
    }
    @Override
    public void push(String url) {
        Jedis con = HttpJedis.getCon();
        con.lpush(PAGELIST,url);
        con.close();
    }

    @Override
    public String pop() {
        Jedis con = HttpJedis.getCon();
        String rpop = con.rpop(PAGELIST);
        con.close();
        return rpop;
    }

    //在redis中存储的string类型的key-value，可以获取当前存储的页数
    public long getCurrentPageNum() {
        Jedis con = HttpJedis.getCon();
        String s = con.get(CURRENTPAGENUM);
        if (s==null){
            con.close();
            return  currentPageNum;
        }
        con.close();
        return Long.valueOf(s);
    }

    public void nextPage(){
        currentPageNum+=50;
        Jedis con = HttpJedis.getCon();
        con.set(CURRENTPAGENUM,String.valueOf(currentPageNum));
        con.close();
    }
}
